package main

import (
	"encoding/json"
	"fmt"
	"github.com/PuerkitoBio/goquery"
	"github.com/gocolly/colly"
    "github.com/gocolly/colly/extensions"
    "os"
    "strings"
    "bufio"
    "io"
    "sort"
    "regexp"
)

func download(){
	file, err := os.OpenFile("./answer.txt",os.O_RDWR|os.O_CREATE,0766)
	if err != nil {
		fmt.Println("err:",err)
	}
	defer file.Close()
	total := 20		//每次限制二十个回答
	i := 0			//记录是第几个答案
	c := colly.NewCollector(func(collector *colly.Collector){
		extensions.RandomUserAgent(collector)
		// collector.Async = true
	})
	c.OnRequest(func(request *colly.Request){
		fmt.Printf("fetch ---> %s \n",request.URL.String())
	})
	c.OnResponse(func(response *colly.Response){
		var f interface{}
		json.Unmarshal(response.Body,&f)	//反序列化

		//找到该问题下的总回答数
		paging := f.(map[string]interface{})["paging"]
		total = int(paging.(map[string]interface{})["totals"].(float64))
		//找到当前url返回数据中的所有答案
		data := f.(map[string]interface{})["data"]
		for k, v := range data.([]interface{}){
			content := v.(map[string]interface{})["content"]
			reader := strings.NewReader(content.(string))
			doc, _ := goquery.NewDocumentFromReader(reader)
			file.Write([]byte(fmt.Sprintf("%d:%s\n",i+k,doc.Find("p").Text())))
		}
	})
	questionID := "319017029"
	for ;i<=total;i+=20{
		url := fmt.Sprintf("https://www.zhihu.com/api/v4/questions/%s/answers?include=data[*].is_normal,admin_closed_comment,reward_info,is_collapsed,annotation_action,annotation_detail,collapse_reason,is_sticky,collapsed_by,suggest_edit,comment_count,can_comment,content,editable_content,voteup_count,reshipment_settings,comment_permission,created_time,updated_time,review_info,relevant_info,question,excerpt,relationship.is_authorized,is_author,voting,is_thanked,is_nothelp,is_labeled,is_recognized,paid_info,paid_info_content;data[*].mark_infos[*].url;data[*].author.follower_count,badge[*].topics&offset=%d&limit=%d&sort_by=updated",questionID,i,20)
		c.Visit(url)
	}

} 

type title struct{
	name string
}

func main(){
	f, err := os.Open("answer.txt")
	if err != nil{
		fmt.Println(err)
	}
	defer f.Close()
	// wc.ReadFile(f)
	rd := bufio.NewReader(f)
	line, err := rd.ReadString('\n')
	if err != nil{
		fmt.Println("err:",err)
	}
	str := string(line)
	// split := []rune("<>《》")

	rg := regexp.MustCompile(`\《.*？》\`)
	param := rg.FindStringSubmatch(str)
	fmt.Println(param)

	// ss := strings.Split(str,"》")
	// fmt.Println(ss)
	// for _, v := range(str){
	// 	for _, v2 := range(split){
	// 		if v == v2{
	// 			fmt.Println(string(v))

	// 		}
	// 	}
	// }
	
}


//单词统计
//这个结构体是为了实现sort接口使用的，因为map如果按照value不好排序
type Pair struct{
	Key string
	Value int
}
type PairList []Pair
func (p PairList) Swap(i,j int) {
	p[i], p[j] = p[j], p[i]
}
func (p PairList) Len() int {
	return len(p)
}
//逆序
func (p PairList) Less(i, j int) bool {
	return p[j].Value < p[i].Value
}
type WordCount map[string]interface{}

//遇到以下符号，对句子进行分割
func SplitByMoreStr(r rune) bool{
	splitSymbol := []rune("<>《》")
	for _,v := range(splitSymbol){
		if r == v{
			return true
		}
	}
	return false
}

func (wc WordCount) SplitAndStatistics(s string){
	dist1 := strings.FieldsFunc(s, SplitByMoreStr)

	for _, v := range(dist1){
		flag := 0
		v = strings.Replace(v," ","",-1)
		for key := range wc{
			if strings.Index(v,key) != -1 {
				wc[key] = wc[key].(int)+1
				flag = 1
			}
		}
		if flag == 0{
			if wc[v] == nil{
				wc[v] = 1
			}else{
				wc[v] = wc[v].(int) + 1
			}
		}
	}
}
// 读取文件的每一行，并进行统计
func (wc WordCount)ReadFile(f *os.File){
	rd := bufio.NewReader(f)
	for{
		line, err := rd.ReadString('\n') //以'\n'为结束符读入一行
		if err != nil || io.EOF == err {
			break
		}
		wc.SplitAndStatistics(line)//切割并统计
	}
}

//这个函数用来排序，展示结果，但没有用到。
func(wc WordCount)AnalysisResut(){
	//将map[string][int] 转成struct 使用sort接口实现排序功能
	pl :=make(PairList,len(wc))
	i:=0
	for k,v :=range(wc){
		pl[i] = Pair{k,v.(int)}
		i++
	}
	sort.Sort(pl)
	for _,pair :=range(pl){
		fmt.Println(pair.Value,pair.Key)
	}

}


